package com.mobilekites;

import java.io.File;

import android.app.Application;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.facebook.SessionDefaultAudience;
import com.mobilekites.api.DfeApi;
import com.mobilekites.api.DfeApiImpl;
import com.mobilekites.api.DfeApiProvider;
import com.mobilekites.downloader.PhotoDownloader;
import com.mobilekites.downloader.PhotoDownloader.PhotoDownloaderImpl;
import com.mobilekites.util.Authenticator;
import com.mobilekites.util.KitesPrefers;
import com.mobilekites.util.KitesPrefers.AdOldPrefers;
import com.mobilekites.util.KitesPrefers.AdOldPrefersImpl;
import com.mobilekites.util.KitesPrefers.KitesPrefersImpl;
import com.sromku.simple.fb.Permission;
import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.SimpleFacebookConfiguration;

public abstract class MobileKitesApp extends Application implements
		DfeApiProvider, ConfigProvider {
	private static MobileKitesApp mKitesApp;

	private RequestQueue mRequestQueue;
	private Cache mCache;
	private ImageLoader mImageLoader;

	@Override
	public void onCreate() {
		super.onCreate();
		mKitesApp = this;

		mCache = new DiskBasedCache(getCacheDir("images"));
		mRequestQueue = new RequestQueue(mCache, createNetwork());
		mImageLoader = new ImageLoader(mRequestQueue, mImageCache);

		mRequestQueue.start();
		// initialized fb
		configFacebook();
		Authenticator.authen(getApplicationContext());
	}

	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	private static File getCacheDir(String name) {
		File file = new File(mKitesApp.getCacheDir(), name);
		file.mkdirs();
		return file;
	}

	public static MobileKitesApp get() {
		return mKitesApp;
	}

	public static Network createNetwork() {
		return new BasicNetwork(new HurlStack());
	}

	private ImageCache mImageCache = new ImageCache() {
		private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(
				10);

		@Override
		public void putBitmap(String url, Bitmap bitmap) {
			cache.put(url, bitmap);
		}

		@Override
		public Bitmap getBitmap(String url) {
			return cache.get(url);
		}
	};


	@Override
	public DfeApi getDfeApi() {
		return new DfeApiImpl(mRequestQueue);
	}

	public PhotoDownloader getPhotoDownloader() {
		return new PhotoDownloaderImpl(getApplicationContext());
	}

	private void configFacebook() {
		// initialize facebook configuration
		Permission[] permissions = new Permission[] { Permission.BASIC_INFO,
				Permission.EMAIL, Permission.USER_PHOTOS,
				Permission.USER_BIRTHDAY, Permission.USER_LOCATION,
				Permission.PUBLISH_ACTION, Permission.PUBLISH_STREAM };
		SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder()
				.setAppId(getKitesConfig().getFacebookAppId())
				.setNamespace(getKitesConfig().getFacebookNameSpace())
				.setPermissions(permissions)
				.setDefaultAudience(SessionDefaultAudience.FRIENDS).build();

		SimpleFacebook.setConfiguration(configuration);
	}

	public KitesPrefers getKitesPrefers() {
		return new KitesPrefersImpl(getApplicationContext());
	}

	public AdOldPrefers getAdOldPrefers() {
		return new AdOldPrefersImpl(getApplicationContext());
	}

}
